﻿using RMS.BLL;
using RMS.Common;
using RMS.EF.Model;
using RMS.Manage.Web.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMS.Manage.Web.Areas.RMS.Controllers
{
    [RMSAuthorize]
    public class RoleController : BaseController
    {
        RoleBll bll = new RoleBll();
        ProgramBll pbll = new ProgramBll();
        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            return View();
        }
        /// <summary>
        /// 列表分页查询
        /// </summary>
        /// <param name="searchModel">筛选条件</param>
        /// <returns></returns>
        public ActionResult ListGetData(S_RMS_Role searchModel)
        {
            PageParamsT<S_RMS_Role> page = SetPageParams(searchModel);
            var result_p = bll.GetByPage(page);
            if (!result_p.IsSuccess)
            {
                return Json(result_p);
            }
            var result = SetPageJson(page, result_p.Data);
            return Json(result);
        }
        /// <summary>
        /// 删除和批量删除
        /// </summary>
        /// <param name="id">Id字符串","分隔</param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            var result = bll.Delete(id);
            return Json(result);
        }
        /// <summary>
        /// 编辑页
        /// </summary>
        /// <param name="id">数据Id</param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            RMS_Role model = new RMS_Role();
            if (!string.IsNullOrWhiteSpace(id))
            {
                var result = bll.GetById(id);
                if (!result.IsSuccess)
                {
                    ViewBag.ErrorMsg = result.Message;
                }
                else
                {
                    model = result.Data;
                }
            }
            return PartialView(model);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model">编辑后的实体</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(RMS_Role model)
        {
            var result = new CommonOutPutVm();
            model.LastUpdateUser = UserId;//记录操作人
            if (string.IsNullOrWhiteSpace(model.Id))
            {
                result = bll.Add(model);
            }
            else
            {
                result = bll.Updata(model);
            }
            return Json(result);
        }
        /// <summary>
        /// 关联菜单页
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <returns></returns>
        public ActionResult Relation_Menu(string id)
        {
            ViewBag.Id = id;
            ViewBag.ErrorMsg = string.Empty;
            #region 获取所有可用程序
            var result_p = pbll.GetAllEnable();
            if (!result_p.IsSuccess)
            {
                ViewBag.ErrorMsg = result_p.Message;
                result_p.Data = new List<RMS_Program>();
            }
            ViewBag.Programs = result_p.Data; 
            #endregion
            return View();
        }
        /// <summary>
        /// 获取角色下的菜单
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Get_RoleMenu_ByRoleIdAndProjectId(string id, string programId)
        {
            #region 获取角色已有菜单Id
            var result_m = bll.Get_RoleMenu_ByRoleIdAndProjectId(id, programId);
            if (!result_m.IsSuccess)
            {
                ViewBag.ErrorMsg = result_m.Message;
                result_m.Data = new List<RMS_Role_Menu>();
            }
            List<RMS_Role_Menu> menus = result_m.Data;
            var data = (from m in menus select m.MenuId).ToList();
            #endregion
            return Json(data);
        }

        /// <summary>
        /// 关联菜单
        /// </summary>
        /// <param name="id">角色Id</param>
        /// <param name="menuId">菜单Id</param>
        /// <param name="programId">项目Id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Relation_Menu(string id, string menuId, string programId)
        {
            CommonOutPutVm result = new CommonOutPutVm();
            #region 菜单Id字符串转集合
            List<string> menuIdints = new List<string>();
            if (!string.IsNullOrWhiteSpace(menuId))
            {
                var menuIdstrs = menuId.Split(',');
                menuIdints = (from t in menuIdstrs select t).ToList();
            } 
            #endregion
            result = bll.Relation_Menu(id, menuIdints, programId, UserId);
            return Json(result);
        }
        
    }
}